package com.zl.basic.Utils;

import java.sql.*;

/**
 * User: zl email:c3tc3tc3t@126.com Date: 14-1-11 Time: 下午2:03 用于进行单元测试提供数据库链接
 */
public class DbUtil {
  public static Connection getConnection() {
    Connection con = null;
    try {
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cms_test", "root", "root");
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }
    return con;
  }


  public static void close(ResultSet rs, Statement stmt, Connection conn) {
    try {
      if (null != rs) {
        rs.close();
        rs = null;
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      try {
        if (null != stmt) {
          stmt.close();
          stmt = null;
        }
      } catch (Exception e) {
        throw new RuntimeException(e);
      } finally {
        try {
          if (null != conn) {
            conn.close();
            conn = null;
          }
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    }
  }

  public static void closeConnection(Connection conn) {
    close(null, null, conn);
  }

  public static void closeStatement(Statement statement) {
    close(null, statement, null);
  }

  public static void closeResultSet(ResultSet rs) {
    close(rs, null, null);
  }
}
